package com.stream;

import com.alibaba.fastjson.JSON;
import com.线程案例.threadLocal.User11;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author: LCG
 * @date: 2022-10-15 22:54:23
 * @description:
 **/
public class StreamTest {
    public static void main(String[] args) {

        test02();

        User11 u1 = new User11();
        u1.setUserName("qqq");
        User11 u2 = new User11();
        u2.setUserName("qqq2");
        User11 u3 = new User11();
        u3.setUserName("qqq");
        User11 u4 = new User11();
        u4.setUserName("qqq4");
        User11 u5 = new User11();
        u5.setUserName("qqq");

        List<User11> user11List=new ArrayList<User11>();
        user11List.add(u1);
        user11List.add(u2);
        user11List.add(u3);
        user11List.add(u4);
        user11List.add(u5);

        Map<String, List<User11>> collect = user11List.stream().collect(Collectors.groupingBy(User11::getUserName));

        user11List.stream().collect(Collectors.groupingBy(User11::getUserName,Collectors.collectingAndThen(Collectors.toList(),x->{
            for (User11 user11 : x) {
                System.out.println("--->"+user11.getUserName());
            }
            return null;
        })));


        test01((x)->{
            System.out.println("---->"+x);
            return "4";});

    }


   public static void test01(Function<String,String> function){
       System.out.println(function.apply("55"));
   }

   /** 测试stream group的时候  如果返回一个实体 会不会覆盖**/
   public static void test02(){
       User11 u1 = new User11();
       u1.setUserName("qqq");
       u1.setPassword("111");
       User11 u2 = new User11();
       u2.setUserName("qqq2");
       User11 u3 = new User11();
       u3.setUserName("qqq");
       u3.setPassword("333");
       User11 u4 = new User11();
       u4.setUserName("qqq4");
       User11 u5 = new User11();
       u5.setUserName("qqq");
       u5.setPassword("555");

       List<User11> user11List=new ArrayList<User11>();
       user11List.add(u1);
       user11List.add(u2);
       user11List.add(u3);
       user11List.add(u4);
       user11List.add(u5);

       Map<String, User11> collect = user11List.stream().collect(Collectors.toMap(User11::getUserName,Function.identity(),(u11,u22)->{
           return u11;
       }, ()->new TreeMap<>(Comparator.reverseOrder())));

       System.out.println(JSON.toJSONString(collect));

   }

}
